package rd.system.toeicphoto {
	
	import com.sff.site.core.INode;
	import com.sff.site.tree.Tree;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author 
	 */
	public class Category  extends Sprite {
		
		public var mcDown:MovieClip;
		private var arrCategory:Array;
		
		public function Category() {
		
			buttonMode = true;
			addEventListener(MouseEvent.CLICK, onClickDownHandler);
			
			txtHeader.autoSize = TextFieldAutoSize.LEFT;
			txtHeader.mouseEnabled = false;
			mcHolder.visible = false;
		}
		
		private function onClickDownHandler(evt:MouseEvent):void {
			if (evt.target.toString().indexOf('CategoryItem_FC') == -1) {
				mcHolder.visible = true;
			} else {
				mcHolder.visible = false;
			}
		}
		
		public function setData(sm:Tree, categories:Array):void {				
			arrCategory = categories;
			
			var categoryItem:MovieClip;			
			var maxWidth:Number = mcBG.width;
			
			for (var i:int = 0; i < arrCategory.length; i ++) {
				categoryItem = new CategoryItem_FC();
				categoryItem.node = sm.getChild(CategoryData(arrCategory[i]).nodeid);
				categoryItem.txtLabel.autoSize = TextFieldAutoSize.LEFT;
				categoryItem.txtLabel.text = CategoryData(arrCategory[i]).name;
				categoryItem.buttonMode = true;
				categoryItem.mouseChildren = false;
				categoryItem.addEventListener(MouseEvent.CLICK, onClickItemHandler);
				categoryItem.y = i * categoryItem.height + 2;
				
				maxWidth = Math.max(maxWidth, categoryItem.width);
				mcHolder.addChild(categoryItem);
				
			}
			
			if (maxWidth > mcBG.width) {
				maxWidth += mcDown.width + 20;
				mcBG.width = maxWidth;
				mcDown.x = maxWidth - mcDown.width - 10;
				
				for (i = 0; i < arrCategory.length; i ++) {
					categoryItem = mcHolder.getChildAt(i) as CategoryItem_FC;
					categoryItem.mcBG.width = maxWidth;
				}
			}
		}
		
		private function onClickItemHandler(evt:MouseEvent):void {
			var node:INode = evt.currentTarget.node;
			if (node) {
				node.activate();
			}
			
			mcHolder.visible = false;
		}
				
		public function updateByNodeId(nodeid:String):void {			
			for (var i:int = 0; i < arrCategory.length; i ++) {
				if (CategoryData(arrCategory[i]).nodeid == nodeid) {					
					txtHeader.text = CategoryData(arrCategory[i]).name;
					break;
				}
			}
		}
	}
}